package jp.tkym.labs.m3d;

import jp.tkym.labs.geom.Vector3f;

/**
 * Projection Coordinate Translator
 * @author takayama
 */
public class ProjectionTranslator{
	private double cot_y;
	private double cot_x;
	
	private ProjectionTranslator(double cot_x, double cot_y){
		this.cot_x = cot_x;
		this.cot_y = cot_y;
	}
	
	public static final ProjectionTranslator createByRatio(double tanOfAngleY, double aspect){
		return new ProjectionTranslator(cotByAngle(tanOfAngleY, aspect), cotByAngle(tanOfAngleY, 1.0d));
	}
	
	public static final ProjectionTranslator createByLength(double width, double height, double distance){
		return new ProjectionTranslator(cot(width, distance), cot(height, distance));
	}
	
	static double cotByAngle(double tan, double aspect){
		return (double) (1 / tan / aspect);
	}
	
	static double cot(double len, double distance){
		double l = len / 2;
		double tan = l / distance;
		return 1 / tan;
	}
	
	public void setAspect(double aspect){
		this.cot_x = cot_y / aspect;
	}
	
	/**
	 * p.z : p.y = distance : s.y
	 * p.z : p.x = distance : s.x
	 * s = [p.x * distance / p.z , p.x * distance / p.z, distance]
	 * v = [p.x / p.z / tanAngle / aspect , p.y / p.z / tanAngle, 1.0f] 
	 * @param p
	 * @return
	 */
	Vector3f translate(Vector3f p){
		return new Vector3f(
				(float) (p.x / p.z * cot_x), 
				(float) (p.y / p.z * cot_y),
				p.z);
	}
}